#!/usr/bin/ruby

def transfac_alignace_data(folder, cutoff = 0)
	file_list	= Dir.entries(Dir.pwd + "/" + folder)
	aa_folder 	= folder + "_aa"

	Dir.mkdir(folder + "/" + aa_folder) unless(file_list.include?(aa_folder))

	meta_file	= File.open(folder + "/_meta.txt")
	
	max = 0
	meta_file.each_line do |l|
		curr_fname, score = l.split("\t")
		score = score.to_i

		max = score 	if(score > max)
		break 			if(score.to_f / max.to_f <= cutoff)

		curr_path 		= folder + "/" + curr_fname
		out_path			= folder + "/" + aa_folder + "/" + curr_fname
		puts curr_path
		`ruby ../transfac.rb ../matrixTFP111.txt #{curr_path} #{out_path}`

	end
end

case ARGV.length
when 0
when 1
	transfac_alignace_data(ARGV[0], 0)

when 2
	transfac_alignace_data(ARGV[0], ARGV[1].to_f)

else
	puts "Invalid number of arguments"
end
